package com.dwd.hrm.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.dwd.hrm.client.LoginUserClient;
import com.dwd.hrm.domain.Employee;
import com.dwd.hrm.domain.LoginUser;
import com.dwd.hrm.domain.Tenant;
import com.dwd.hrm.dto.TenantEnteringDto;
import com.dwd.hrm.mapper.EmployeeMapper;
import com.dwd.hrm.mapper.TenantMapper;
import com.dwd.hrm.query.TenantQuery;
import com.dwd.hrm.service.ITenantService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.dwd.hrm.util.AjaxResult;
import com.dwd.hrm.util.PageList;
import com.dwd.hrm.utils.ValidUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author wendi
 * @since 2021-03-02
 */
@Service
public class TenantServiceImpl extends ServiceImpl<TenantMapper, Tenant> implements ITenantService {
    @Autowired
    private TenantMapper tenantMapper;
    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private LoginUserClient loginUserClient;


    /**
     * 分页
     *
     * @param query
     * @return
     */
    @Override
    public PageList<Tenant> queryPage(TenantQuery query) {
        Page<Tenant> page = new Page<Tenant>(query.getPage(), query.getRows());
        List<Tenant> lists = tenantMapper.queryPage(page, query);
        return new PageList<>(page.getTotal(), lists);
    }

    /**
     * 机构入驻
     *
     * @param tenantEnteringDto
     */
    @Override
    public void entering(TenantEnteringDto tenantEnteringDto) {

        //先保存用户信息表login_user  返回主键
        //存中间表user_meal表   login_user_id和meal_id    为了关联meal
        LoginUser loginUser = new LoginUser();
        loginUser.setMealId(tenantEnteringDto.getMealId());
        loginUser.setPassword(tenantEnteringDto.getEmployee().getPassword());
        loginUser.setType(0);
        loginUser.setUsername(tenantEnteringDto.getEmployee().getUsername());
        AjaxResult ajaxResult = loginUserClient.addOrUpdate(loginUser);
        //获取loginUserId
        Long loginUserId = Long.valueOf(ajaxResult.getResultObj().toString());
        if (ajaxResult.isSuccess()) {
            //存员工表emp  employee  返回主键
            Employee employee = tenantEnteringDto.getEmployee();
            //设置其他属性  应该设置常量  机构类型
            employee.setState(5);
            employee.setInputTime(new Date());
            employee.setRealName(employee.getUsername());
            employee.setType(0);
            employee.setLoginId(loginUserId);
            //自动返回主键
            employeeMapper.insert(employee);
            //存tenant   admin_id就是emp_id  返回主键
            Tenant tenant = tenantEnteringDto.getTenant();
            //设置管理员
            tenant.setAdminId(employee.getId());
            //返回主键
            tenantMapper.insert(tenant);
            //修改emp  保存里面的tenant_id
            employee.setTenantId(tenant.getId());
            employeeMapper.updateById(employee);
        }

    }
}
